package com.gp.node.operator;

import java.util.List;

import com.gp.node.INode;
import com.utils.Random;

// abstract class for binary operation nodes (always have 2 child and therefore implement IFather)
// override functions description can be found at the interface IFather 
public abstract class IOperatorBinary implements IFather {
	
	private static final long serialVersionUID = -4103933262406167886L;
	// the nodes two child
	protected INode left =null;
	protected INode right =null;
	
	// return the left child
	public INode getLeft() {
		return left;
	}

	// set the left child
	public void setLeft(INode left) {
		this.left = left;
	}

	// return the right child
	public INode getRight() {
		return right;
	}

	// set the right child
	public void setRight(INode right) {
		this.right = right;
	}

	// set nodes child
	protected void setByCopy(IOperatorBinary op){
		 if(op.getLeft() != null){
			 setLeft(op.getLeft().copy());
		 }
		 
		 if(op.getRight() != null){
			 setRight(op.getRight().copy());
		 }		
	}
	
	@Override
	public void fillFathers(List<IFather> ls) {
		ls.add(this);
		left.fillFathers(ls);
		right.fillFathers(ls);
	}
	 
	@Override
	public void fillAll(List<INode> ls) {
		ls.add(this);
		left.fillAll(ls);
		right.fillAll(ls);
	}
	 
	@Override
	public void setChild(INode node) {
		int num = Random.nextInt(2);
		if(num == 0){
			left = node;
		}else{
			right = node;
		}
	}
}
